package 单周赛.history;

import java.util.*;

/**
 *
 */
public class 第302场单周赛 {

    public static void main(String[] args) {

        int[] nums = {1, 3, 2, 1, 3, 2, 2};
        System.out.println(numberOfPairs(nums));

        //[368,369,307,304,384,138,90,279,35,396,114,328,251,364,300,191,438,467,183]
        //输出：
        //765
        //预期：
        //835
        int[] nums2 = {368, 369, 307, 304, 384, 138, 90, 279, 35, 396, 114, 328, 251, 364, 300, 191, 438, 467, 183};
        System.out.println(maximumSum(nums2));


        String[] nums3 = {"64333639502", "65953866768", "17845691654", "87148775908", "58954177897", "70439926174", "48059986638", "47548857440", "18418180516", "06364956881", "01866627626", "36824890579", "14672385151", "71207752868"};
        int[][] queries = {{6, 1}};
        System.out.println(Arrays.toString(smallestTrimmedNumbers(nums3, queries)));

    }

    public static int[] numberOfPairs(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        int count1 = 0, count2 = 0;
        for (Integer num : map.keySet()) {
            count1 += map.get(num) / 2;
            count2 += map.get(num) % 2;
        }
        return new int[]{count1, count2};
    }

    public static int maximumSum(int[] nums) {
        Integer[] nums2 = new Integer[nums.length];
        for (int i = 0; i < nums.length; i++) {
            nums2[i] = nums[i];
        }

        Arrays.sort(nums2, (o1, o2) -> {
            int n1 = 0, n2 = 0, t1 = o1, t2 = o2;
            while (t1 > 0) {
                n1 += t1 % 10;
                t1 /= 10;
            }
            while (t2 > 0) {
                n2 += t2 % 10;
                t2 /= 10;
            }
            return n1 == n2 ? o2 - o1 : n2 - n1;
        });
        int maxNum = -1;
        for (int i = 0; i < nums2.length - 1; i++) {
            int n1 = 0, n2 = 0, t1 = nums2[i], t2 = nums2[i + 1];
            while (t1 > 0) {
                n1 += t1 % 10;
                t1 /= 10;
            }
            while (t2 > 0) {
                n2 += t2 % 10;
                t2 /= 10;
            }
            if (n1 == n2) {
                maxNum = Math.max(maxNum, nums2[i] + nums2[i + 1]);
            }
        }
        return maxNum;
    }

    /**
     * 每个查询将数组根据大小排序
     * 排序之前用map存储字符串对应的索引位置
     *
     * @param nums
     * @param queries
     * @return
     */
    public static int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {
        int[] idxs = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {

            String[] nums2 = new String[nums.length];
            Map<String, List<Integer>> map = new HashMap<>();
            for (int j = 0; j < nums.length; j++) {
                nums2[j] = nums[j].substring(nums[j].length() - queries[i][1]);

                List<Integer> list = map.getOrDefault(nums2[j], new ArrayList<>());
                list.add(j);
                map.put(nums2[j], list);
            }

            Arrays.sort(nums2, (o1, o2) -> o1.compareTo(o2));

            int count = queries[i][0];
            for (String num : nums2) {
                List<Integer> list = map.get(num);
                if (list == null) continue;
                // nums2中可能存在重复元素
                map.remove(num);
                list.sort((o1, o2) -> o1 - o2);
                boolean flag = false;
                for (int r = 0; r < list.size(); r++) {
                    count--;
                    if (count == 0) {
                        idxs[i] = list.get(r);
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
        }
        return idxs;
    }
}
